<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表
           @selection-change="selsChange" 选的不一样就会触发
        -->
        <el-table :data="adoptOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="orderSn" label="订单号" width="160" sortable>
            </el-table-column>
            <el-table-column prop="digest" label="摘要" width="200" sortable>
            </el-table-column>
            <el-table-column prop="price" label="价格" width="120" sortable>
            </el-table-column>
            <!--:formatter="formatState"-->
            <el-table-column prop="state" label="状态" width="150"  sortable>
                <template scope="scope">
                    <span style="color: yellowgreen" v-if="scope.row.state==0">待支付</span>
                    <span style="color: orange" v-else-if="scope.row.state==1">待配送</span>
                    <span style="color: pink" v-else-if="scope.row.state==2">待确认</span>
                    <span style="color: green" v-else-if="scope.row.state==3">完成</span>
                    <span style="color: red" v-else-if="scope.row.state==-1">取消</span>
                </template>
            </el-table-column>
            <!--关联查询-后台查询出来后，前台只需要通过关系属性.要显示的字段名就ol-->
            <el-table-column prop="address" label="地址" width="300" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <!--如果没有选中就不能使用:disabled="this.sels.length===0"-->
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增或编辑界面-->
        <el-dialog :title="title" :visible.sync="adoptOrderFormVisible" :close-on-click-modal="false">
            <!--:model="adoptOrderForm 模型绑定-->
            <!--:rules="adoptOrderFormRules" 校验规则-->
            <!--ref="adoptOrderForm" 给该表单定义一个引用名-->
            <el-form :model="adoptOrderForm" label-width="80px" :rules="adoptOrderFormRules" ref="adoptOrderForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="adoptOrderForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="标识" prop="sn">
                    <el-input v-model="adoptOrderForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="adoptOrderForm.state">
                        <el-radio class="radio" :label="0">正常</el-radio>
                        <el-radio class="radio" :label="-1">停用</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="adoptOrderFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="save" :loading="adoptOrderLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                adoptOrders: [],
                totals: 0,
                page: 1,
                pageSize:10,
                listLoading: false,
                sels: [],//列表选中列

                adoptOrderFormVisible: false,//新增界面是否显示
                adoptOrderLoading: false,
                adoptOrderFormRules: {
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入标识', trigger: 'blur' }
                    ]
                },
                title:"",
                adoptOrderForm: {
                    id:null,
                    name: '',
                    sn: '',
                    state: 0,
                    manager: {},
                    parent: {}
                }
            }
        },
        methods: {
            // //状态显示转换-不能上色
            // formatState: function (row, column) {
            // 	return row.state == 0 ? '正常' : row.state == -1 ? '禁用' : '未知';
            // },
            search() {
                //查询完，原来分页条件已经没用了，要跑到第一页去
                this.page = 1;
                this.getAdoptOrders();
            }
            ,
            handleCurrentChange(val) {
                this.page = val;
                this.getAdoptOrders();
            },
            //获取领养订单列表
            getAcquisition() {
                let para = {
                    currentPage: this.page,
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;
                this.$http.post("/petAcquisitionOrder",para)
                    .then(result=>{
                        result = result.data; //取出data这一层
                        this.total = result.total;
                        this.adoptOrders = result.rows;
                        this.listLoading = false;
                    })
                    .catch(result=>{
                        alert("系统错误！")
                    })
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.adoptOrderForm = Object.assign({}, row); //回显
                this.adoptOrderFormVisible = true; // 显示编辑框
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.adoptOrderForm = { //清空
                    id:null,
                    name: '',
                    sn: '',
                    state: 0,
                    manager: null,
                    parent: null
                }

                this.adoptOrderFormVisible = true; //显示新增框
            },
            //保存
            save: function () {
                // 表单上定义了ref="adoptOrderForm"
                this.$refs.adoptOrderForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认保存吗？', '温馨提示', {}).then(() => {
                            this.adoptOrderLoading = true;
                            //拷贝一份新数据
                            let para = Object.assign({}, this.adoptOrderForm);
                            //对数据进行额外处理
                            if(para.parent){
                                let arrys = para.parent;//[2,5]
                                //后台要的是adoptOrder对象
                                para.parent = {'id':arrys[arrys.length-1]}
                            }
                            this.$http.put("/adoptOrder",para)
                                .then(result => {
                                    result = result.data
                                    //去除忙等框
                                    this.adoptOrderLoading = false;
                                    //做提示
                                    if(result.success){
                                        this.$message({
                                            message: '保存成功',
                                            type: 'success'
                                        });
                                    }else{
                                        this.$message({
                                            message: result.message,
                                            type: 'error'
                                        });
                                    }
                                    //关闭对话框
                                    this.adoptOrderFormVisible =false;
                                    //重新查询数据
                                    this.getAdoptOrders();
                                })
                                .catch(result=>{
                                    alert("系统错误！");
                                    this.adoptOrderLoading = false;
                                })
                            ;
                        });
                    }
                });
            },
            //所有选中对象都会传进来
            selsChange: function (sels) {
                this.sels = sels;
                console.log(this.sels)
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let url = "/adoptOrder/"+row.id;
                    this.$http.delete(url)
                        .then(result=>{
                                result = result.data; //{sucess:false,message}
                                //去除忙等框
                                this.listLoading = false;

                                if(result.success){
                                    this.page = 1;
                                    //提示
                                    this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                    //重新查询数据
                                    this.getAdoptOrders();
                                }else{
                                    //提示
                                    this.$message({
                                        message: result.message,
                                        type: 'error'
                                    });
                                    //重新查询数据
                                    this.getAdoptOrders();
                                }
                            }

                        )
                        .catch(result=>{
                            //去除忙等框
                            this.listLoading = false;
                            //提示
                            this.$message({
                                message: '系统错误！',
                                type: 'error'
                            });
                        })

                }).catch(() => {

                });
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id); //[1,2,3]
                console.log(ids);
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //传递数组参数
                    this.$http.patch("/adoptOrder",ids)
                        .then(result=>{
                            result = result.data;
                            this.listLoading = false;
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            this.page =1; //删除后跳转第一页
                            this.getAdoptOrders();
                        })
                        .catch(result=>{
                            alert("系统错误")
                        })
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getAcquisition();
        }
    }

</script>

<style scoped>

</style>